using System;

namespace foundation
{
    [AttributeUsage(AttributeTargets.Field)]
    public sealed class RecycleAttribute : Attribute
    {
        public object value;
        public bool isRecycleObject;
        public RecycleAttribute(object defaultvalue, bool isRecycleObject = false)
        {
            this.value = defaultvalue;
            this.isRecycleObject = isRecycleObject;
        }

        public static void recycle(object obj)
        {
            if (null == obj) return;
            var type = typeof(RecycleAttribute);
            foreach (var target in obj.GetType().GetFields())
            {
                foreach (RecycleAttribute attr in target.GetCustomAttributes(type, true))
                {
                    if (attr.isRecycleObject == false)
                    {
                        target.SetValue(obj, attr.value);
                    }
                    else
                    {
                        recycle(target.GetValue(obj));
                    }

                }
            }
        }
    }

}